package com.moon;

import java.util.ArrayList;
import java.util.List;

import cn.yueliangbaba.R;

import com.util.PreferencesUtils;
import com.widget.TouchImageView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class GuideActivity extends BaseActivity {

	public ViewPager viewPager; // android-support-v4中的滑动组件
	private List<ImageView> imageViews; // 滑动的图片集合

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		
		init();
	}

	private void init() {
		imageViews = new ArrayList<ImageView>();
		viewPager = (ViewPager) findViewById(R.id.vp);
		imageViews.clear();

		for (int i = 0; i < 3; i++) 
		{
			ImageView imageView = new ImageView(this);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			imageView.setImageResource(R.drawable.guide0 + i);
			imageViews.add(imageView);
			
			if(i == 2)
			{
				imageView.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						PreferencesUtils.putBoolean(GuideActivity.this, "is_init", true);
						startActivity(new Intent(GuideActivity.this,MainActivity.class));
						finish();
					}
				});
			}
		}

		viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
		viewPager.setCurrentItem(0);
	}
	/**
	 * 填充ViewPager页面的适配器
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyAdapter extends PagerAdapter {

		public MyAdapter() {
		}

		@Override
		public int getCount() {
			// return imageResId.length;
			return imageViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(imageViews.get(arg1));
			return imageViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {

		}
	}
}
